// pages/ceremony/photo-album.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    photos: [
      { id: 1, url: '/images/memorial1.jpg', description: '纪念照片1', date: '2023-10-15' },
      { id: 2, url: '/images/memorial2.jpg', description: '纪念照片2', date: '2023-10-15' },
      { id: 3, url: '/images/memorial3.jpg', description: '纪念照片3', date: '2023-10-15' },
      { id: 4, url: '/images/memorial4.jpg', description: '纪念照片4', date: '2023-10-14' },
      { id: 5, url: '/images/memorial5.jpg', description: '纪念照片5', date: '2023-10-14' },
      { id: 6, url: '/images/memorial6.jpg', description: '纪念照片6', date: '2023-10-13' }
    ],
    selectedPhotos: [],
    isEditMode: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 页面加载时的初始化
  },

  /**
   * 返回上一页
   */
  onBack() {
    wx.navigateBack();
  },

  /**
   * 上传照片
   */
  onUploadPhoto() {
    wx.chooseImage({
      count: 9,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFilePaths = res.tempFilePaths;
        wx.showToast({
          title: '照片上传成功',
          icon: 'success'
        });
        
        // 这里可以调用上传API
        console.log('上传照片:', tempFilePaths);
      }
    });
  },

  /**
   * 选择照片
   */
  onSelectPhoto(e) {
    const photoId = e.currentTarget.dataset.id;
    const { selectedPhotos, isEditMode } = this.data;
    
    if (isEditMode) {
      const index = selectedPhotos.indexOf(photoId);
      if (index > -1) {
        selectedPhotos.splice(index, 1);
      } else {
        selectedPhotos.push(photoId);
      }
      
      this.setData({
        selectedPhotos: selectedPhotos
      });
    } else {
      // 查看照片详情
      this.onViewPhoto(photoId);
    }
  },

  /**
   * 查看照片详情
   */
  onViewPhoto(photoId) {
    const photo = this.data.photos.find(p => p.id === photoId);
    if (photo) {
      wx.previewImage({
        urls: [photo.url],
        current: photo.url
      });
    }
  },

  /**
   * 切换编辑模式
   */
  onToggleEditMode() {
    this.setData({
      isEditMode: !this.data.isEditMode,
      selectedPhotos: []
    });
  },

  /**
   * 删除选中照片
   */
  onDeletePhotos() {
    const { selectedPhotos } = this.data;
    
    if (selectedPhotos.length === 0) {
      wx.showToast({
        title: '请选择要删除的照片',
        icon: 'none'
      });
      return;
    }
    
    wx.showModal({
      title: '确认删除',
      content: `确定要删除选中的${selectedPhotos.length}张照片吗？`,
      success: (res) => {
        if (res.confirm) {
          // 这里可以调用删除API
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          });
          
          this.setData({
            isEditMode: false,
            selectedPhotos: []
          });
        }
      }
    });
  },

  /**
   * 下载选中照片
   */
  onDownloadSelected() {
    const { selectedPhotos } = this.data;
    
    if (selectedPhotos.length === 0) {
      wx.showToast({
        title: '请选择要下载的照片',
        icon: 'none'
      });
      return;
    }
    
    wx.showToast({
      title: '开始下载照片',
      icon: 'loading'
    });
    
    // 这里可以实现批量下载逻辑
    setTimeout(() => {
      wx.showToast({
        title: '下载完成',
        icon: 'success'
      });
    }, 2000);
  },

  /**
   * 下载照片
   */
  onDownloadPhoto(e) {
    const photoId = e.currentTarget.dataset.id;
    const photo = this.data.photos.find(p => p.id === photoId);
    
    if (photo) {
      wx.downloadFile({
        url: photo.url,
        success: (res) => {
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: () => {
              wx.showToast({
                title: '保存成功',
                icon: 'success'
              });
            },
            fail: () => {
              wx.showToast({
                title: '保存失败',
                icon: 'none'
              });
            }
          });
        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '纪念相册',
      path: '/pages/ceremony/photo-album'
    };
  }
})
